package top.callback.inf.beautyleaf.server.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.callback.inf.beautyleaf.server.common.page.Page;
import top.callback.inf.beautyleaf.server.common.page.Paging;
import top.callback.inf.beautyleaf.server.dao.LeafTagDao;
import top.callback.inf.beautyleaf.server.entity.LeafTagDO;
import top.callback.inf.beautyleaf.server.model.LeafTagModel;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签服务
 *
 * @author Pengyu Gan
 * CreateDate 2021/4/8
 */
@Service
@Transactional(readOnly = true, rollbackFor = {Exception.class})
public class LeafTagService {

    @Autowired
    private LeafTagDao leafTagDao;

    /**
     * 新增标签
     * @param tagModel {@link LeafTagModel} 标签信息模型
     * @return true 成功 false 失败
     */
    @Transactional
    public boolean addTag(LeafTagModel tagModel) {
        LeafTagDO tag = new LeafTagDO();
        BeanUtils.copyProperties(tagModel, tag);
        return leafTagDao.addTag(tag) != 0;
    }

    /**
     * 编辑标签
     * @param tagModel {@link LeafTagModel} 标签信息模型
     * @return true 成功 false 失败
     */
    @Transactional
    public boolean editTag(LeafTagModel tagModel) {
        LeafTagDO tag = new LeafTagDO();
        BeanUtils.copyProperties(tagModel, tag);
        return leafTagDao.editTag(tag) != 0;
    }

    /**
     * 删除标签
     * @param tagId 标签 ID
     * @return true 成功 false 失败
     */
    @Transactional
    public boolean removeTag(long tagId) {
        return leafTagDao.removeTag(tagId) != 0;
    }

    /**
     * 查询指定 ID 的标签
     * @param tagId 标签 ID
     * @return {@link LeafTagModel} 标签信息模型
     */
    public LeafTagModel findById(long tagId) {
        LeafTagDO tag = leafTagDao.findById(tagId);
        if (null == tag) {
            throw new IllegalArgumentException("标签不存在");
        }
        LeafTagModel tagModel = new LeafTagModel();
        BeanUtils.copyProperties(tag, tagModel);
        return tagModel;
    }

    /**
     * 分页查询标签
     * @param pageNo 页码
     * @param pageSize 数据量
     * @return 分页标签信息列表
     */
    public Page<LeafTagModel> findAll(long pageNo, long pageSize) {
        Paging paging = new Paging(pageNo, (int) pageSize);
        List<LeafTagDO> tags = leafTagDao.findAll(paging);
        List<LeafTagModel> tagModels = new ArrayList<>();
        if (null != tags && !tags.isEmpty()) {
            tagModels = tags.stream().map(tag -> {
                LeafTagModel tagModel = new LeafTagModel();
                BeanUtils.copyProperties(tag, tagModel);
                return tagModel;
            }).collect(Collectors.toList());
        }
        return new Page<>(paging, tagModels);
    }

    /**
     * 启用/禁用 标签
     * @param tagId 标签 ID
     * @return true 成功 false 失败
     */
    @Transactional
    public boolean switchTag(long tagId) {
        LeafTagDO tag = leafTagDao.findById(tagId);
        if (null == tag) {
            throw new IllegalArgumentException("标签不存在");
        }
        tag.setActive(!tag.isActive());
        return leafTagDao.editTag(tag) != 0;
    }
}
